package com.vitas.coin.vm

import android.annotation.SuppressLint
import android.util.Log
import androidx.fragment.app.Fragment
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.snap.util.startAct
import com.vitas.coin.data.bean.VoiceCateBean
import com.vitas.coin.manager.PlayManager
import com.vitas.coin.resp.HomeResp
import com.vitas.coin.ui.act.UploadAct
import com.vitas.coin.ui.adapter.ViewPageAdapter
import com.vitas.coin.ui.fm.LocalFrg
import com.vitas.coin.ui.fm.MusicFrg
import kotlinx.coroutines.launch

class CateVM : BaseMusicVM() {


    val musicName = MutableLiveData("--")

    val homeResp = HomeResp()


    var musicCateList: ArrayList<VoiceCateBean> = ArrayList()
    var mFragmentList: ArrayList<Fragment> = ArrayList()


    @SuppressLint("NotifyDataSetChanged")
    fun loadData() {
        viewModelScope.launch {
            Log.e("tagg", "request tabs")
            homeResp.requestCateRecommendList {
                if (it != null) {
                    musicCateList.addAll(it.cates)
                    mFragmentList.add(LocalFrg())
                    for (i in musicCateList.indices) {
                        val mItemData: VoiceCateBean = musicCateList[i];
                        val mMusicFrg: MusicFrg = MusicFrg.newInstance(mItemData.name, mItemData.id)
                        mFragmentList.add(mMusicFrg)
                    }
                    musicCateList.add(0, VoiceCateBean(0L, "本机", ""))
                    mViewPageAdapter.notifyDataSetChanged()
                } else {

                }
            }
        }
    }


    fun onClickAddUpload() {
        startAct(UploadAct::class.java) {
            putExtra(UploadAct.PARENT_ID, 0L)
        }
    }

    fun onClickStartOrPause() {
        PlayManager.getInstance().onClickPlayButton()
    }


    val mViewPageAdapter by lazy { ViewPageAdapter(actionAct.invoke(), mFragmentList) }


}